using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Yonkly.Controllers;

namespace Yonkly
{
    /// <summary>
    /// Checks the User's authentication using FormsAuthentication
    /// and redirects to the Login Url for the application on fail
    /// </summary>
    public class RequiresAuthenticationAttribute : ActionFilterAttribute
    {

        public override void OnActionExecuting(FilterExecutingContext filterContext)
        {
            //redirect if not authenticated
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                //use the current url for the redirect
                string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath;

                ////send them off to the login page
                string redirectUrl = string.Format("?ReturnUrl={0}", redirectOnSuccess);
                string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;
                filterContext.HttpContext.Response.Redirect(loginUrl, true);
                //filterContext.Cancel = true;
                //throw new UnauthorizedAccessException();
            }

        }
    }

    //public class ActionInfo
    //{
    //    public string ActionName { get; set; }
    //}

    //public delegate void ActionDirector (ActionInfo action);

    //public class RedirectToActionOnError : ActionFilterAttribute
    //{
    //    public ActionDirector  Action { get; set; }
    //    public override void OnActionExecuted(FilterExecutedContext filterContext)
    //    {
            
    //    }

    //}

    //public class RedirectToActionOnError : ActionFilterAttribute
    //{
    //    public string ActionName { get; set; }
    //    public override void OnActionExecuted(FilterExecutedContext filterContext)
    //    {

    //        //RedirectToAction(filterContext, ActionName);
    //        if (filterContext.Exception.GetType() == typeof(UnauthorizedAccessException) ||
    //            filterContext.Exception.InnerException.GetType() == typeof(UnauthorizedAccessException))
    //        {
    //            //
    //            RedirectToAction(filterContext, ActionName);
    //        }
    //    }

    //    protected void RedirectToAction(FilterExecutedContext filterContext, string actionName)
    //    {
    //        if (String.IsNullOrEmpty(actionName))
    //        {
    //            throw new ArgumentException("Value cannot be null or empty", "actionName");
    //        }
    //        RouteValueDictionary valuesDictionary = new RouteValueDictionary();
    //        valuesDictionary.Add("action", actionName);
    //        RedirectToAction(filterContext, valuesDictionary);
    //    }

    //    protected virtual void RedirectToAction(FilterExecutedContext filterContext, RouteValueDictionary values)
    //    {

    //        VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(((BaseController)filterContext.Controller).ControllerContext, values);
    //        string target = null;
    //        if (vpd != null)
    //        {
    //            target = vpd.VirtualPath;
    //        }
    //        filterContext.HttpContext.Response.Redirect(target);
    //    }

    //}
}